const api = "http://localhost:3000"

function addU() {
    location.href = './add.html'
}

function filterU() {
    render()
}

function editU(id) {
    location.href = `./up.html?id=${id}`
}

function delU(id) {
    axios.delete(`${api}/user/${id}`).then(() => {
        location.href = './index.html'
    })
}

function render() {
    let keyword = document.querySelector("#keyword").value
    axios.get(`${api}/users?keyword=${keyword}`).then(res => {
        document.querySelector('tbody').innerHTML = ''
        res.data.forEach(item => {
            document.querySelector('tbody').innerHTML +=
                `
            <tr>
                        <td>${item.id}</td>
                        <td>${item.uname}</td>
                        <td>${item.uage}</td>
                        <td>
                            <input type="button" value="编辑" onclick="editU(${item.id})">
                            <input type="button" value="删除" onclick="delU(${item.id})">
                        </td>
                    </tr>
            `
        });
    })
}

render()